﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Security.Cryptography;

namespace Utilities
{
    public class Utilities
    {
        public Utilities()
        {

        }

        /// <summary>
        /// Ham ma hoa
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <param name="key"></param>
        /// <param name="useHashing"></param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        /// Ham giai ma
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <param name="key"></param>
        /// <param name="useHashing"></param>
        /// <returns></returns>
        public static string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        public static List<ulong> SplitStringDeviceToInt(string sTrakID)
        {
            List<ulong> ulList = new List<ulong>();
            try
            {
                if (!string.IsNullOrEmpty(sTrakID))
                {
                    string[] arrTrkID = sTrakID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    ulong ulTrackId = 0;
                    for (int i = 0; i < arrTrkID.Length; i++)
                    {
                        if (ulong.TryParse(arrTrkID[i], out ulTrackId))
                        {
                            ulList.Add(ulTrackId);
                        }
                    }
                    return ulList;
                }

            }
            catch (Exception)
            {

                throw;
            }
            return ulList;
        }
        public static UInt32 DeviceID(ulong m_uTrackerID)
        {
            return (UInt32)(m_uTrackerID & 0x00000000ffffffff); 
        }
        public UInt32 LogServerID(ulong m_uTrackerID)
        {
            return (UInt32)((m_uTrackerID >> 48) & 0x000000000000ffff); 
        }
        public UInt32 TrackerTypeID(ulong m_uTrackerID)
        {
             return (UInt32)((m_uTrackerID >> 32) & 0x000000000000ffff); 
        }
    }
}
